package com.verisette.ncos.service.card.bizunit;

import java.io.File;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.MasterMember;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.persistence.domain.TransCustomerOrder;
import com.verisette.ncos.web.card.bean.CardCreateBean;
import com.verisette.ncos.web.utilities.SendNotifyMailThread;

public class NotifyApproverByEmailBU extends CommonBusinessUnit {
	private Logger logger = Logger.getLogger(NotifyApproverByEmailBU.class);
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
// TODO Auto-generated method stub

		MasterUserProfile sender = (MasterUserProfile)processContext.getUserBean();
		
		CardCreateBean cardCreateBean= (CardCreateBean) processContext.getValueObject();
		TransCustomerOrder order = null;
		if(cardCreateBean!=null){
			order = (TransCustomerOrder)cardCreateBean.getOrder();
		}
		
//		TransCustomerOrder order = (TransCustomerOrder)processContext.getValueObject();
		SendNotifyMailThread sendMailThread = new SendNotifyMailThread();
		if(order.getStatus().equals(ProjectConstant.STATUS_WAIT_PRINT)){
			sendMailThread.setMailTemplate("mail-print-request");
			sendMailThread.setSubjectContent("รอดำเนินการพิมพ์");
			
			StringBuilder fullPoPathBd = new StringBuilder(ProjectConstant.APP_ORDER_PO_FOLDER);
			fullPoPathBd.append("\\").append(order.getPoPath());
			
			StringBuilder fullPDFPreviewPathBd = new StringBuilder(ProjectConstant.APP_ORDER_PDF_PREVIEW_FOLDER);
			fullPDFPreviewPathBd.append("\\").append(order.getPdfPath());
			
			try{
				File f = null;
				f = new File(fullPoPathBd.toString());
				if(f!=null){
					sendMailThread.addAttachment(f);
				}
				
				f = new File(fullPDFPreviewPathBd.toString());
				if(f!=null){
					sendMailThread.addAttachment(f);
				}
			}catch(Exception ex){
				logger.error("Attachments failed.",ex);
			}
			
			MasterUserProfile creator = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, order.getCreatedUspId());
			sendMailThread.addEmailCcReceivers(creator.getUspEmail());
			
		} else {
			sendMailThread.setMailTemplate("mail-approve-request");
		}
		sendMailThread.setOrder(order);
		sendMailThread.setSender(sender);
		
		MasterUserProfile creator = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, order.getCreatedUspId());
		MasterUserProfile receiver = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, sender.getApproverUspId());
		
		sendMailThread.setReceiver(receiver);
		
		MasterMember member = order.getMasterMember();
		String requesterName = member.getMmbFnameTh(), requesterLname = member.getMmbLnameTh();
		if( StringUtils.isBlank(requesterName)){
			requesterName = member.getMmbFnameEn();
			requesterLname = member.getMmbLnameEn();
		}
		
		String name = "ไม่ทราบชื่อ";
		if(StringUtils.isNotBlank(receiver.getFirstName())){
			name = receiver.getFirstName();
			if(StringUtils.isNotBlank(receiver.getLastName())){
				name = name + " " + receiver.getLastName();
			}
		}
		
		sendMailThread.addContentParam(name);
		StringBuilder sbf = new StringBuilder(creator.getFirstName());
		sbf.append(" ").append(creator.getLastName());
		sbf.append(" (").append(requesterName);
		sbf.append(" ").append(requesterLname).append(") ");
		sendMailThread.addContentParam(sbf.toString());
		
		sendMailThread.start();
	}
}